<!--
<template lang="jade">
.title
  h1 123
  el-button(type='primary', icon='delete' ,class="handle-del mr10", @click="$router.push('/push/new')")批量删除
  el-table(:data='data' border,style="width: 100%",ref="multipleTable" )
    el-table-column(prop='lastsno', label='Content')
    el-table-column(prop='node_id', label='CreateAt', width="150")
    el-table-column(prop='opertype', label=' SendAt', width="180")
    el-table-column(prop='lastsno', label='ID', width="100")
</template>
-->
<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-menu"></i> 数据源管理</el-breadcrumb-item>
                <el-breadcrumb-item>数据源列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="handle-box">
            <el-button type="primary" icon="delete" class="handle-del mr10" @click="delAll">批量删除</el-button>
            <el-button type="primary" icon="delete" class="handle-del mr10" @click="addOne">添加</el-button>

        </div>
        <div>
<el-dialog title="新增数据源" :visible.sync="dialogFormVisible">
  <div class="form-box">
            <el-form ref="form" :model="form" label-width="80px">
                <el-form-item label="数据源名称">
                    <el-input v-model="form.data_name"></el-input>
                </el-form-item>
                <el-form-item label="数据库类型">
                    <el-select v-model="form.db_type" placeholder="请选择">
                        <el-option key="bbk" label="oracle" value="bbk"></el-option>
                        <el-option key="xtc" label="mysql" value="xtc"></el-option>
                        <el-option key="imoo" label="sqlserver" value="imoo"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="数据库IP">
                    <el-input v-model="form.db_ip"></el-input>
                </el-form-item>
                <el-form-item label="数据库端口">
                    <el-input v-model="form.db_port"></el-input>
                </el-form-item>
                <el-form-item label="数据库用户">
                    <el-input v-model="form.db_user"></el-input>
                </el-form-item>
                <el-form-item label="数据库密码">
                    <el-input v-model="form.db_passwd"></el-input>
                </el-form-item>
                <el-form-item label="数据库示例名">
                    <el-input v-model="form.db_name"></el-input>
                </el-form-item>
                  <el-form-item label="数据库采集sql">
                    <el-input type="textarea" v-model="form.db_sql"></el-input>
                </el-form-item>
                <el-form-item label="数据源状态">
                    <el-radio-group v-model="form.status">
                        <el-radio label="未开启"></el-radio>
                        <el-radio label="开启"></el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">提交</el-button>
                    <el-button @click="dialogFormVisible = false">取消</el-button>
                </el-form-item>
            </el-form>
        </div>
</el-dialog>
       </div>
        <el-table :data="data" border style="width: 100%" ref="multipleTable" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="data_name" label="数据源名称" width="120">
            </el-table-column>
            <el-table-column prop="db_type" label="数据库类型" width="120">
            </el-table-column>
            <el-table-column prop="db_ip" label="数据库IP" width="120">
            </el-table-column>
            <el-table-column prop="db_port" label="数据库端口" width="120">
            </el-table-column>
            <el-table-column prop="status" label="数据源状态" width="120">
            </el-table-column>
            <el-table-column label="操作" width="180">
                <template slot-scope="scope">
                    <el-button size="small"
                            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="small" type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                    @current-change ="handleCurrentChange"
                    layout="prev, pager, next"
                    :total="1000">
            </el-pagination>
        </div>
    </div>
</template>

<script>

    //import service from '../../utils/request'
    //import {reqeustPost} from '../../utils/request'
    import service from '@/utils/request'
    import {requestPost} from '@/utils/request'
    import qs from 'qs'

    export default {
        data() {
            return {
                url: './static/vuetable.json',
                tableData: [{
                    id: '-1',
                    data_name: "test",
                    db_type: "mysql",
                    db_ip: "127.0.0.1",
                    db_port: "3306",
                    db_user: "test",
                    db_passwd: "test",
                    db_name: "test",
                    db_sql: "test",
                    status: "未开启"
                }],
                cur_page: 1,
                multipleSelection: [],
                select_cate: '',
                select_word: '',
                del_list: [],
                is_search: false,
                dialogFormVisible: false,
                form: {
                    data_name: 'a',
                    db_type: 'mysql',
                    db_ip: '1',
                    db_port: '1',
                    db_user: '1',
                    db_passwd: '1',
                    db_name: '1',
                    db_sql: '1',
                    status: '未开启'
                }
            }
        },
        created(){
            this.getData();
        },
        computed: {
            data(){
                const self = this;
                return self.tableData;
                /*
                .filter(function(d){
                    let is_del = false;
                    for (let i = 0; i < self.del_list.length; i++) {
                        if(d.name === self.del_list[i].name){
                            is_del = true;
                            break;
                        }
                    }
                    if(!is_del){
                        if(d.address.indexOf(self.select_cate) > -1 &&
                            (d.name.indexOf(self.select_word) > -1 ||
                            d.address.indexOf(self.select_word) > -1)
                        ){
                            return d;
                        }
                    }
                })
                */
            }
        },
        methods: {
            handleCurrentChange(val){
                this.cur_page = val;
                this.getData();
            },
            getData(){
                let self = this;
                //reqeustPost('/ioc_req', {SRVC_ID:"90101005",active:4}).then((res) => {
                service.post('/ioc_req', {SRVC_ID:"90101005",active:4}).then((res) => {
                    self.tableData = res.data.data;
                })

            },
            search(){
                this.is_search = true;
            },
            formatter(row, column) {
                return row.address;
            },
            filterTag(value, row) {
                return row.tag === value;
            },
            handleEdit(index, row) {
                this.$message('编辑第'+(index+1)+'行');
                this.form = this.tableData[index]
                this.dialogFormVisible = true;
                this.action = 1;
                console.log('row',row);
            },
            handleDelete(index, row) {
                this.$message.error('删除第'+(index+1)+'行');
                var ids_arry = [this.tableData[index].id]
                this.$message.error('删除第'+(index+1)+'行  111');
                /*
                this.$axios.post('/ioc_req',
                    {
                        SRVC_ID:"90101008",
                        ids: ids_arry
                    }).then((res) => {

                        this.$axios.post('/ioc_req', {SRVC_ID: "90101005"}).then((res) => {
                        this.tableData = res.data.data;
                    })
                })
                */
                 //reqeustPost('/ioc_req',
                 service.post('/ioc_req',
                    {
                        SRVC_ID:"90101008",
                        ids: ids_arry
                    }).then((res) => {

                        //reqeustPost('/ioc_req',
                        this.getData()
                })

                 this.$message.error('删除第'+(index+1)+'行  222');

            },
            delAll(){
                const self = this,
                    length = self.multipleSelection.length;
                let str = '';
                self.del_list = self.del_list.concat(self.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += self.multipleSelection[i].name + ' ';
                }
                self.$message.error('删除了'+str);
                self.multipleSelection = [];
            },

            addOne() {
                this.dialogFormVisible = true;
                this.form = {}
                this.action = 0;
            },
            onSubmit() {
                         //var status = 0;
                //if (self.form.status == '开启') {
                //    status = 1;
                //}
                this.$message.success('已提交！1');
                var db_type_dict = {
                    'mysql': 1,
                    "oracle": 2,
                    "sqlserver": 3
                }

                this.$message.success('已提交！2');
                var db_type_value = db_type_dict[this.form.db_type];
                if (db_type_value == undefined) {
                    db_type_value = 1
                }

                var ds_id = 0;
                if (this.action == 0) {
                    ds_id = '-1';
                } else {
                    ds_id = this.form.id;
                }

                this.$message.success('已提交！3444444444');
                /*
                this.$axios.post('/ioc_req',
                    {
                    SRVC_ID:"90101004",
                    id: ds_id,
                    data_name: this.form.data_name,
                    db_type: db_type_value,
                    db_ip: this.form.db_ip,
                    db_port: this.form.db_port,
                    db_user: this.form.db_user,
                    db_passwd: this.form.db_passwd,
                    db_name: this.form.db_name,
                    db_sql: this.form.db_sql,
                    status: '-1'
                    }).then((res) => {
                    this.$axios.post('/ioc_req',
                    {
                        SRVC_ID: "90101005"
                    }).then((res) => {
                        this.tableData = res.data.data;
                        this.dialogFormVisible = false;
                    })
                })
                */
                service.post('/ioc_req',
                //reqeustPost('/ioc_req',
                    {
                    SRVC_ID:"90101004",
                    id: ds_id,
                    data_name: this.form.data_name,
                    db_type: db_type_value,
                    db_ip: this.form.db_ip,
                    db_port: this.form.db_port,
                    db_user: this.form.db_user,
                    db_passwd: this.form.db_passwd,
                    db_name: this.form.db_name,
                    db_sql: this.form.db_sql,
                    status: '-1'
                    }).then((res) => {
                        this.getData();
                        this.dialogFormVisible = false;

                    })
                this.$message.success('已提交！5');
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            }
        }
    }
</script>

<style scoped>
.handle-box{
    margin-bottom: 20px;
}
.handle-select{
    width: 120px;
}
.handle-input{
    width: 300px;
    display: inline-block;
}
</style>
